package com.qbw.app.module.home.adapter;

import android.content.Context;
import android.view.ViewGroup;

import androidx.recyclerview.widget.RecyclerView;

import com.qbw.app.model.Coolant;
import com.qbw.app.model.Location;
import com.qbw.app.model.ProblemStatus;
import com.qbw.app.model.TireData;
import com.qbw.app.model.Travel;
import com.qbw.app.model.Vehicle;
import com.qbw.app.model.Voltage;
import com.qbw.app.module.home.holder.HomeActionHolder;
import com.qbw.app.module.home.holder.HomeBatteryMilleageHolder;
import com.qbw.app.module.home.holder.HomeCurrentVehicleHolder;
import com.qbw.app.module.home.holder.HomeInfoHolder;
import com.qbw.app.module.home.holder.HomeLocationHolder;
import com.qbw.app.module.home.holder.HomeStatusItemHolder;
import com.qbw.app.module.home.holder.HomeStatussHolder;
import com.qbw.app.module.home.holder.HomeTravelHolder;
import com.qbw.app.module.home.holder.HomeWaitNewTravelHolder;
import com.qbw.app.module.home.holder.HomeWaringMessageHolder;

import static com.qbw.app.module.home.adapter.HomeViewType.VT_ACTION;
import static com.qbw.app.module.home.adapter.HomeViewType.VT_INFO;
import static com.qbw.app.module.home.adapter.HomeViewType.VT_LAST_TRAVEL;
import static com.qbw.app.module.home.adapter.HomeViewType.VT_LOCATION;
import static com.qbw.app.module.home.adapter.HomeViewType.VT_STATUS_ITEM;
import static com.qbw.app.module.home.adapter.HomeViewType.VT_STATUS_ITEMS;
import static com.qbw.app.module.home.adapter.HomeViewType.VT_WARNING_INFO;

/*
 *
 * @author QBW
 * @date 2019/8/17
 */
public class HomeAdapter extends BaseHomeAdapter {

    public HomeAdapter(Context context, Object listener) {
        super(context, listener);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder viewHolder;
        switch (viewType) {
            case VT_INFO:
                viewHolder = new HomeInfoHolder(mContext, parent);
                break;
            case VT_STATUS_ITEM:
            case HomeViewType.VT_STATUS_ITEM_BATTERY:
            case HomeViewType.VT_STATUS_ITEM_COOLANT:
            case HomeViewType.VT_STATUS_ITEM_PROBLEM:
            case HomeViewType.VT_STATUS_ITEM_TIRE:
                viewHolder = new HomeStatusItemHolder(mContext, parent);
                break;
            case VT_LAST_TRAVEL:
                viewHolder = new HomeTravelHolder(mContext, parent, mListener);
                break;
            case VT_LOCATION:
                viewHolder = new HomeLocationHolder(mContext, parent);
                break;
            case VT_ACTION:
                viewHolder = new HomeActionHolder(mContext, parent);
                break;
            case HomeViewType.VT_WAIT_NEW_TRAVEL:
                viewHolder = new HomeWaitNewTravelHolder(mContext, parent);
                break;
            case HomeViewType.VT_CURRENT_VEHICLE:
                viewHolder = new HomeCurrentVehicleHolder(mContext, parent, mListener);
                break;
            case VT_STATUS_ITEMS:
                viewHolder = new HomeStatussHolder(mContext, parent);
                break;
            case VT_WARNING_INFO:
                viewHolder = new HomeWaringMessageHolder(mContext, parent);
                break;
            case HomeViewType.VT_BATTERY_MILLEAGE:
                viewHolder = new HomeBatteryMilleageHolder(mContext, parent);
                break;
            default:
                viewHolder = super.onCreateViewHolder(parent, viewType);
                break;
        }
        return viewHolder;
    }

    @Override
    public int getItemViewType(Object t) {
        if (t instanceof Vehicle.VehicleList) {
            return VT_INFO;
        } else if (t instanceof Voltage || t instanceof Coolant || t instanceof ProblemStatus || t instanceof TireData) {
            return VT_STATUS_ITEM;
        } else if (t instanceof Travel) {
            return VT_LAST_TRAVEL;
        } else if (t instanceof Location) {
            return VT_LOCATION;
        } else if (t instanceof HomeActionHolder.Data) {
            return VT_ACTION;
        } else if (t instanceof HomeCurrentVehicleHolder.Data) {
            return HomeViewType.VT_CURRENT_VEHICLE;
        } else if (t instanceof HomeStatussHolder.Data) {
            return VT_STATUS_ITEMS;
        } else if (t instanceof HomeWaringMessageHolder.Data) {
            return VT_WARNING_INFO;
        } else if (t instanceof HomeBatteryMilleageHolder.Data) {
            return HomeViewType.VT_BATTERY_MILLEAGE;
        }
        return super.getItemViewType(t);
    }

    public void removeChildByObjectType(Class c) {
        int cc = getChildCount();
        Object o;
        for (int i = 0; i < cc; i++) {
            o = getChild(i);
            if (o.getClass() == c) {
                removeChild(i);
                break;
            }
        }
    }

    public int getChildPositionByClass(Class c) {
        int cc = getChildCount();
        int ti = -1;
        Object o;
        for (int i = 0; i < cc; i++) {
            o = getChild(i);
            if (o.getClass() == c) {
                ti = i;
                break;
            }
        }
        return ti;
    }
}
